/*
 * LevelSelector.java
 *
 * Created on 9. juillet 2007, 09:47
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pacman;

import java.awt.BorderLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;

/**
 * The level selector window
 */
public class LevelSelector extends JFrame {
    private cCore parent;
    
    private JList LevelList;
    private JButton btOk;
    private JButton btQuit;
    
    private ArrayList<String> MapPathList;
    
    /**
     * Creates a new instance of LevelSelector and show its
     * @param parent The cCore game manager associated
     */
    public LevelSelector(cCore parent) {
        this.parent=parent;
        
        this.setSize(300,300);
        this.setTitle("Tuxman");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        BorderLayout Layout=new BorderLayout();
        JPanel container =new JPanel();
        container.setLayout(Layout);
        container.setSize(300,300);
        
        LevelList=new JList();
        LevelList.setSize(200,300);
        container.add(LevelList,BorderLayout.CENTER);
        
        JPanel btPanel=new JPanel();
        
        btOk=new JButton();
        btOk.setText("OK");
        btOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btOk_Clicked(evt);
            }
        });
        btPanel.add(btOk);
        
        btQuit=new JButton();
        btQuit.setText("Quit");
        btQuit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btQuit_Clicked(evt);
            }
        });
        btPanel.add(btQuit);
        
        
        container.add(btPanel,BorderLayout.SOUTH);
        
        this.add(container);
        
        LoadMaps();
        
        this.setVisible(true);
    }
    
    private void btQuit_Clicked(java.awt.event.MouseEvent evt){
        System.exit(0);
    }
    
    private void btOk_Clicked(java.awt.event.MouseEvent evt){
        try {
            parent.LoadMap(MapPathList.get(LevelList.getSelectedIndex()));
        } catch (ParserNotFoundException ex) {
            ex.printStackTrace();
        }
        
        Gui gui=new Gui(parent);
        
        gui.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt){
                Gui_Closing();
            }
        });
        
        parent.addView(gui);
        
        this.setVisible(false);
    }
    
    private void Gui_Closing(){
        parent.Terminate();
        this.setVisible(true);
    }
    
    private void LoadMaps(){
        File mapsPath = new File("maps/");
        String filename;
        ArrayList<iParser> ParserList=parent.getParserList();
        iParser parser;
        
        MapPathList=new ArrayList<String>();
        
        Vector MapList=new Vector();
        
        for(int i=0;i<mapsPath.listFiles().length;i++){
            filename=mapsPath.list()[i];
            
            for(int j=0;j<ParserList.size();j++){
                parser=ParserList.get(j);
                if(filename.substring(filename.length()-3,filename.length()).equalsIgnoreCase(parser.getSupportedExt())){
                    MapList.add(mapsPath.listFiles()[(mapsPath.list().length-1)-i].getName().substring(0,mapsPath.listFiles()[(mapsPath.list().length-1)-i].getName().length()-4));
                    MapPathList.add(mapsPath.listFiles()[(mapsPath.list().length-1)-i].getPath());
                }
            }
        }
        
        LevelList.setListData(MapList);
        
        LevelList.setSelectedIndex(0);
    }
    
}
